package com.zqx.bridge.utils;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;

/**
 * description: xml文件解析工具类
 * @author zqx
 * @date 2018/09/03
 */
public class XMLUtil {

    public static Object getBean(String args)
    {

        try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document document = documentBuilder.parse(new File("src/main/java/com/zqx/bridge/config/config.xml"));
            NodeList classNameNodeList = document.getElementsByTagName("className");
            Node classNameNode = null;
            //获取第一个包含类名的节点，即扩充抽象类
            if(args.equals("image")) {
                classNameNode = classNameNodeList.item(0).getFirstChild();
            }
            //获取第二个包含类名的节点，即具体实现类
            else if(args.equals("os"))
            {
                classNameNode = classNameNodeList.item(1).getFirstChild();
            }
            String className = classNameNode.getNodeValue();
            Class<?> aClass = Class.forName(className);
            Object o = aClass.newInstance();
            return o;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}
